package mulis;

import dvsdk.GlobalConfig;
import dvsdk.client.PhotoWheelListner;
import dvsdk.form.IPhoto;
import dvsdk.form.IPhotoGallery;
import dvsdk.util.Util;
import java.util.List;
import javax.swing.ImageIcon;

/**
 *
 * @author Develop
 */
public class PhotoViewer extends javax.swing.JFrame implements IPhoto,IPhotoGallery {
    private IPhoto core;
    private int selected;
    private int max=0;
    /** Creates new form PhotoViewer */
    public PhotoViewer(IPhoto core, int selected) {
        this.selected=selected;
        this.max=core.getPreImages().size()-1;
        this.core=core;
        initComponents();
        postInit();
    }
    
    private void postInit(){
        photoPanel.addMouseWheelListener(new PhotoWheelListner(this));
        
        Util.fillMiniPhotoGallery(photoGalleryPanel, GlobalConfig.photoMiniSize, core.getPreImages(), this);
        core.generateFullImages(photoButton.getWidth());
        repaintFullImage();
        //this.setSize(photoButton.getWidth()+photoGalleryPanel.getWidth(), photoButton.getHeight()+photoGalleryPanel.getHeight());
    }
    
    private void repaintFullImage() {
        Util.fillFullPhotoInGallery(photoButton, core.getFullImage(selected));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        photoPanel = new javax.swing.JPanel();
        photoButton = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        photoGalleryPanel = new javax.swing.JPanel();
        btnAdd = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(mulis.MulisApp.class).getContext().getResourceMap(PhotoViewer.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        photoPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        photoPanel.setName("photoPanel"); // NOI18N
        photoPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 0, 0));

        photoButton.setText(resourceMap.getString("photoButton.text")); // NOI18N
        photoButton.setBorderPainted(false);
        photoButton.setContentAreaFilled(false);
        photoButton.setFocusPainted(false);
        photoButton.setFocusable(false);
        photoButton.setMargin(new java.awt.Insets(0, 0, 0, 0));
        photoButton.setMaximumSize(new java.awt.Dimension(640, 480));
        photoButton.setMinimumSize(new java.awt.Dimension(230, 240));
        photoButton.setName("photoButton"); // NOI18N
        photoButton.setPreferredSize(new java.awt.Dimension(640, 480));
        photoButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                photoButtonMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                photoButtonMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                photoButtonMouseReleased(evt);
            }
        });
        photoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                photoButtonActionPerformed(evt);
            }
        });
        photoPanel.add(photoButton);

        jScrollPane4.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        jScrollPane4.setAutoscrolls(true);
        jScrollPane4.setDoubleBuffered(true);
        jScrollPane4.setMinimumSize(new java.awt.Dimension(112, 112));
        jScrollPane4.setName("jScrollPane4"); // NOI18N
        jScrollPane4.setPreferredSize(new java.awt.Dimension(112, 112));

        photoGalleryPanel.setName("photoGalleryPanel"); // NOI18N
        photoGalleryPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEADING));

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/mulis/resources/add64.png"))); // NOI18N
        btnAdd.setToolTipText(resourceMap.getString("btnAdd.toolTipText")); // NOI18N
        btnAdd.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        btnAdd.setContentAreaFilled(false);
        btnAdd.setMargin(new java.awt.Insets(0, 0, 0, 0));
        btnAdd.setMaximumSize(new java.awt.Dimension(100, 100));
        btnAdd.setMinimumSize(new java.awt.Dimension(100, 100));
        btnAdd.setName("btnAdd"); // NOI18N
        btnAdd.setPreferredSize(new java.awt.Dimension(100, 100));
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        photoGalleryPanel.add(btnAdd);

        jScrollPane4.setViewportView(photoGalleryPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(photoPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 640, Short.MAX_VALUE)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 640, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(photoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void photoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_photoButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_photoButtonActionPerformed

    private void photoButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_photoButtonMouseClicked

    }//GEN-LAST:event_photoButtonMouseClicked

    private void photoButtonMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_photoButtonMousePressed
        // TODO add your handling code here:
    }//GEN-LAST:event_photoButtonMousePressed

    private void photoButtonMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_photoButtonMouseReleased
        if (evt.getX()>=photoButton.getWidth()/2) {
            //System.out.println("Before");
            showPrewPhoto();
        }
        if (evt.getX()<photoButton.getWidth()/2) {
            //System.out.println("After");
            showNextPhoto();
        }
    }//GEN-LAST:event_photoButtonMouseReleased

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed

    }//GEN-LAST:event_btnAddActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JButton photoButton;
    private javax.swing.JPanel photoGalleryPanel;
    private javax.swing.JPanel photoPanel;
    // End of variables declaration//GEN-END:variables


    @Override
    public void showBigPhoto(int selected) {
        this.selected=selected;
        repaintFullImage();
    }

    @Override
    public List<ImageIcon> getPreImages() {
        return core.getPreImages();
    }

    @Override
    public ImageIcon getFullImage(int num) {
        return getFullImage(num);
    }

    @Override
    public void generateFullImages(int size) {
        core.generateFullImages(size);
    }

    @Override
    public List<ImageIcon> getOrigImages() {
        return core.getOrigImages();
    }

    @Override
    public void showNextPhoto() {
        selected=(selected+1>max)?0:selected+1;
        repaintFullImage();
    }

    @Override
    public void showPrewPhoto() {
        selected=(selected-1<0)?max:selected-1;
        repaintFullImage();
    }
}